﻿(function ($) {
    var opusAutoCompleteSettings = {
        url: '/Ref/Languages',
        maxResults: 10,
        alternate: true,
        showName: true,
        width: 600
    };

    var methods = {
        init: function (options) {

            return this.each(function () {
                if (options) {
                    $.extend(opusAutoCompleteSettings, options);
                }

                $(this).combogrid({
                    url: opusAutoCompleteSettings.url,
                    colModel: [
                        { 'columnName':'id', 'hide':true, 'width':0, 'label':'Id', 'align':'left' },
                        { 'columnName': 'code', 'width': 10, 'label': 'Code', 'align': 'left' },
                        { 'columnName': 'name', 'hide': (!opusAutoCompleteSettings.showName), 'width': 30, 'label': 'Name', 'align': 'left' },
                        { 'columnName': 'description', 'width': 60, 'label': 'Description', 'align': 'left' }
                    ],
                    width: opusAutoCompleteSettings.width,
                    alternate: opusAutoCompleteSettings.alternate,
                    select: function (event, ui) {
                        $(this).val(ui.item.description).attr('data-opus-id', ui.item.id).attr('data-opus-code', ui.item.code);
                        return false;
                    }
                });
            });
        },
        destroy: function () {
            return this.each(function () {
                $(this).unbind('.opusAutoComplete');
            });
        }
    };

    $.fn.opusAutoComplete = function (method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.opusAutoComplete');
        }
    };
})(jQuery);
